package com.xmy.system.service.impl;

import com.xmy.common.constant.UserConstants;
import com.xmy.common.core.domain.entity.SysUser;
import com.xmy.common.core.domain.entity.WeChatUser;
import com.xmy.common.utils.StringUtils;
import com.xmy.system.mapper.WeChatUserMapper;
import com.xmy.system.service.ISysWeChatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ISysWeChatUserServiceImpl implements ISysWeChatUserService {
    @Autowired
    private WeChatUserMapper weChatUserMapper;

    @Override
    public WeChatUser getUserByOpenId(String openid) {

        return weChatUserMapper.getUserByOpenId(openid);
    }

    @Override
    public void registerUser(WeChatUser user) {
        weChatUserMapper.insertUser(user);
    }

    @Override
    public void updateUser(WeChatUser weChatUser) {
        weChatUserMapper.updateUser(weChatUser);
    }

    @Override
    public WeChatUser getUserById(Long userId) {
        return weChatUserMapper.getUserById(userId);
    }

    @Override
    public boolean updateUserAvatar(Long id, String avatar) {
        WeChatUser weChatUser = new WeChatUser();
        weChatUser.setId(id);
        weChatUser.setAvatarUrl(avatar);
        return weChatUserMapper.updateUser(weChatUser)>0;
    }

    @Override
    public int updateUserProfile(WeChatUser currentUser) {
        return weChatUserMapper.updateUser(currentUser);
    }

    @Override
    public boolean checkPhoneUnique(WeChatUser currentUser) {
        Long userId = StringUtils.isNull(currentUser.getId()) ? -1L : currentUser.getId();
        WeChatUser info = weChatUserMapper.checkPhoneUnique(currentUser.getPhone());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
